/**
 * Created by kevin on 2017/7/3.
 */
var model= require('./model');

var DocClassifies = model.createModel({
    name: String,
    recommend : Boolean,
    bySystem : {type : Boolean,default:false},
    ctime : {type : Date,default:Date.now}
},'DocClassifies');

DocClassifies.getAllexceptSystem = function(callback) {
    DocClassifies.find({bySystem:{ $ne:true}},null,{ lean: true ,sort:{ctime:-1}},callback);
};

DocClassifies.getAll = function(callback) {
    DocClassifies.find({},null,{ lean: true ,sort:{ctime:-1}},callback);
};

DocClassifies.getById = function (id,callback) {
    DocClassifies.findById(id,callback);
};

DocClassifies.getRecommendList = function (cb) {
    DocClassifies.find({recommend : true}).sort({ _id:-1}).lean(true).limit(3).exec(cb);
};

DocClassifies.createUnique = function (doc,callback) {
    DocClassifies.findOne({'name':doc.name},function (err,d) {
        if (d) {
            if (callback) {
                callback(new Error('该名字已被占用',null));
            }
        } else  {
            DocClassifies.create(doc,callback);
        }
    });
};

DocClassifies.edit = function (id,doc,callback) {
    DocClassifies.findByIdAndUpdate(id, doc, callback);
};

function createSystemDocClassify() {
    DocClassifies.createUnique({
        name : '打包资讯',
        recommend:true,
        bySystem:true
    })
}

DocClassifies.getByName = function (name,cb) {
    DocClassifies.findOne({'name':name},cb);
};

createSystemDocClassify();

module.exports = DocClassifies;